package com.alks.function.controller.biocontrol.deptwork;

import com.alibaba.excel.util.StringUtils;
import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.biocontrol.deptwork.CutSlipDto;
import com.alks.function.data.request.biocontrol.deptwork.CutInRequest;
import com.alks.function.data.request.biocontrol.deptwork.CutStkRequest;
import com.alks.function.service.biocontrol.deptwork.ICpCutService;
import com.alks.function.service.biocontrol.deptwork.ICutInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@ApiRestController("/bioDeptWrok")
@Api(value = "生控作业", tags = "生控部-生控作业-冲刀出库作业")
@RoleCheck(prgIds = "140121")
@RequiredArgsConstructor
public class CutOutController {
    private final ICutInService service;
    private final ICpCutService cpInfoService;
    private final String slipType = "O";

    @PutMapping("/getCutOutHeadQuery")
    @ApiOperation(value = "冲刀出库单查询-可条件查询", tags = "生控部-生控作业-冲刀出库作业")
    public ResponseInfo getCutOutHeadQuery(@RequestBody CutStkRequest request) {
        return service.getCutHeadQuery(request, slipType);
    }

    @PutMapping("/CutOutHeadCheck")
    @ApiOperation(value = "冲刀出库单检核-更新", tags = "生控部-生控作业-冲刀出库作业")
    public ResponseInfo cutOutHeadCheck(@RequestParam @ApiParam("订单号") String slipNo) {
        return service.cutHeadCheck(slipNo, slipType);
    }

    @PostMapping("/getCutOutDetQuery")
    @ApiOperation(value = "冲刀出库单明细查询-条件查询", tags = "生控部-生控作业-冲刀出库作业")
    public ResponseInfo getCutOutDetQuery(@RequestParam @ApiParam("订单号") String slipNo) {
        CutSlipDto dto = service.getCutDetQuery(slipNo, slipType);
        return ResponseInfo.ok(dto);
    }

    @PutMapping("/CutOutAdd")
    @ApiOperation(value = "冲刀出库单创建-新增", tags = "生控部-生控作业-冲刀出库作业")
    public ResponseInfo cutOutAdd(@RequestBody CutInRequest request) {
        /*冲刀号检测*/
        if (StringUtils.isBlank(request.getCutNo())) {
            throw new ServiceErrorException("冲刀号不能为空");
        }
        cpInfoService.cutNoCheck(request.getCutNo());
        String slipNo = service.cutAdd(request, slipType);
        return ResponseInfo.ok(slipNo);
    }

    @PutMapping("/CutOutUp")
    @ApiOperation(value = "冲刀出库单更新-更新", tags = "生控部-生控作业-冲刀出库作业")
    public ResponseInfo cutOutUp(@RequestBody CutInRequest request) {
        /*冲刀号检测*/
        if (StringUtils.isBlank(request.getSlipNo())){
            throw new ServiceErrorException("订单号不能为空");
        }if (StringUtils.isBlank(request.getCutNo())){
            throw new ServiceErrorException("冲刀号不能为空");
        }
        cpInfoService.cutNoCheck(request.getCutNo());
        String slipNo = service.cutUp(request, slipType);
        return ResponseInfo.ok(slipNo);
    }

    @PutMapping("/CutOutDetPart")
    @ApiOperation(value = "取冲刀部位-更新", tags = "生控部-生控作业-冲刀出库作业")
    public ResponseInfo cutOutDetPart(@RequestBody CutInRequest request) {
        /*冲刀号检测*/
        if (request.getSlipNo()==null){
            if (StringUtils.isNotBlank(request.getCutNo())){
                cpInfoService.cutNoCheck(request.getCutNo());
            }else {
                throw new ServiceErrorException("顶栏冲刀号不能为空");
            }
        }
        String slipNo = service.cutDetPart(request, slipType);
        return ResponseInfo.ok(slipNo);
    }

    @PostMapping("/CutOutDelete")
    @ApiOperation(value = "整单删除-删除", tags = "生控部-生控作业-冲刀出库作业")
    public ResponseInfo cutOutDelete(@RequestParam @ApiParam("订单号") String slipNo) {
        return service.cutDelete(slipNo, slipType);
    }

    @PutMapping("/CutOutDetDelete")
    @ApiOperation(value = "冲刀出库明细删除-删除", tags = "生控部-生控作业-冲刀出库作业")
    public ResponseInfo cutOutDetDelete(@RequestParam @ApiParam("订单号") String slipNo,
                                        @RequestParam @ApiParam("序号") String seqNo) {
        return service.cutDetDelete(slipNo, seqNo, slipType);
    }
}
